package com.gitee.ice1938.module.admin.app.manage.appversiondetail;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Order;
import org.springframework.jdbc.core.BaseRepository;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.QueryInfo;
import org.springframework.stereotype.Repository;

/**
 * AppVersionDetailController
 * 
 * 
 * @author ice1938@qq.com 2019-11-29 14:21:39
 *
 */
@Repository
public class AppVersionDetailRepository extends BaseRepository {

	@Resource
	private JdbcTemplate jdbcTemplate;

	private QueryInfo querySql(Map<String, Object> param, Pageable pageable, Order order) {
		String keyword = param.get("q_search_key") == null ? null : param.get("q_search_key").toString().trim();
		List<Object> params = new ArrayList<Object>();
		StringBuilder sql = new StringBuilder("SELECT ");
		// 物理主键
		sql.append("avd.id,");
		// APPMD5
		sql.append("avd.app_md5,");
		// app_package
		sql.append("avd.app_package,");
		// 应用名称
		sql.append("avd.app_name,");
		// 版本号
		sql.append("avd.version_code,");
		// 版本名称
		sql.append("avd.version_name,");
		// 最小支持版本
		sql.append("avd.min_sdk_version,");
		// 目标版本
		sql.append("avd.target_sdk_version,");
		// 程序路径 jakrabbit path
		sql.append("avd.app_path,");
		// 程序图标路径 jakrabbit path
		sql.append("avd.icon_path,");
		// 发布标识:0:未发布;1:发布
		sql.append("avd.publish_flag,");
		// 强制标识:0:不强制;1:强制
		sql.append("avd.force_flag,");
		// APK 大小 单位byte
		sql.append("avd.app_size,");
		// 评分
		sql.append("avd.hi_score,");
		// 下载数
		sql.append("avd.download_count,");
		// 广告标识
		sql.append("avd.ad_flag,");

		// 图片path jakrabbit path
		sql.append("avd.img1_path,");
		// 图片path jakrabbit path
		sql.append("avd.img2_path,");
		// 图片path jakrabbit path
		sql.append("avd.img3_path,");
		// 图片path jakrabbit path
		sql.append("avd.img4_path,");
		// 图片path jakrabbit path
		sql.append("avd.img5_path,");
		// 更新时间
		sql.append("avd.update_time");
		//
		sql.append(" FROM app_version_detail as avd where 1=1 ");
		// 模糊查询模版
		if (keyword != null && !keyword.isEmpty()) {
			sql.append(" and avd.app_md5 like ?");
			params.add("%" + keyword + "%");
		}

		String sqlCount = builderCountSql(sql.toString());
		if (order != null) {
			sql.append(" ORDER BY ");
			sql.append(underscoreName(order.getProperty()));
			sql.append(" ");
			sql.append(order.getDirection());
		}
		if (pageable != null) {
			sql.append(" LIMIT ");
			sql.append(pageable.getPageNumber() * pageable.getPageSize());
			sql.append(", ");
			sql.append(pageable.getPageSize());
		}
		return new QueryInfo(sql.toString(), sqlCount, params);

	}

	public Page<AppVersionDetailEntity> findData(Map<String, Object> param, Pageable pageable, Order order) {
		QueryInfo sqlinfo = querySql(param, pageable, order);

		List<AppVersionDetailEntity> content = jdbcTemplate.query(sqlinfo.getSql(), sqlinfo.getParams().toArray(),
		        BeanPropertyRowMapper.newInstance(AppVersionDetailEntity.class));
		if (pageable != null) {
			long total = jdbcTemplate.queryForObject(sqlinfo.getCountSql(), sqlinfo.getParams().toArray(), Long.class);
			return new PageImpl<AppVersionDetailEntity>(content, pageable, total);
		}
		return new PageImpl<AppVersionDetailEntity>(content);
	}

	public Page<Map<String, Object>> queryData(Map<String, Object> param, Pageable pageable, Order order) {
		QueryInfo sqlinfo = querySql(param, pageable, order);

		List<Map<String, Object>> content = jdbcTemplate.queryForList(sqlinfo.getSql(), sqlinfo.getParams().toArray());
		if (pageable != null) {
			long total = jdbcTemplate.queryForObject(sqlinfo.getCountSql(), sqlinfo.getParams().toArray(), Long.class);
			return new PageImpl<Map<String, Object>>(content, pageable, total);
		}
		return new PageImpl<Map<String, Object>>(content);
	}

	public int update(AppVersionDetailEntity data, String userLoginName) {
		StringBuilder sql = new StringBuilder("UPDATE app_version_detail  SET  ");
		List<Object> params = new ArrayList<>();
		// APPMD5
		sql.append("app_md5 = ?,");
		params.add(data.getAppMd5());
		// app_package
		sql.append("app_package = ?,");
		params.add(data.getAppPackage());
		// 应用名称
		sql.append("app_name = ?,");
		params.add(data.getAppName());
		// 评分
		sql.append("hi_score = ?,");
		params.add(data.getHiScore());
		// 下载数
		sql.append("download_count = ?,");
		params.add(data.getDownloadCount());
		// 广告标识
		sql.append("ad_flag = ?,");
		params.add(data.getAdFlag());
		// 版本号
		sql.append("version_code = ?,");
		params.add(data.getVersionCode());
		// 版本名称
		sql.append("version_name = ?,");
		params.add(data.getVersionName());
		// 版本号
		sql.append("version_code = ?,");
		params.add(data.getVersionCode());
		// 版本名称
		sql.append("version_name = ?,");
		params.add(data.getVersionName());
		// 最小支持版本
		sql.append("min_sdk_version = ?,");
		params.add(data.getMinSdkVersion());
		// 目标版本
		sql.append("target_sdk_version = ?,");
		params.add(data.getTargetSdkVersion());

		sql.append(" WHERE ");
		// APPMD5
		sql.append("app_md5 = ?");
		params.add(data.getAppMd5());
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int insert(AppVersionDetailEntity data, String userLoginName) {

		StringBuilder sql = new StringBuilder("INSERT INTO app_version_detail ( ");
		List<Object> params = new ArrayList<>();
		// APPMD5
		sql.append("app_md5 ,");
		params.add(data.getAppMd5());
		// app_package
		sql.append("app_package ,");
		params.add(data.getAppPackage());
		// 应用名称
		sql.append("app_name ,");
		params.add(data.getAppName());
		// 广告标识
		sql.append("ad_flag ,");
		params.add(data.getAdFlag());
		// 版本号
		sql.append("version_code ,");
		params.add(data.getVersionCode());
		// 版本名称
		sql.append("version_name ,");
		params.add(data.getVersionName());
		//最小支持版本
 		sql.append("min_sdk_version ,"); 
        params.add(data.getMinSdkVersion());
  		//目标版本
 		sql.append("target_sdk_version ,"); 
        params.add(data.getTargetSdkVersion());
		// 程序图标
    	//程序路径 jakrabbit path
 		sql.append("app_path ,"); 
        params.add(data.getAppPath());
  		//程序图标路径 jakrabbit path
 		sql.append("icon_path"); 
        params.add(data.getIconPath());

		sql.append(" ) VALUES (?,?,?,?,?,?,?,?,?,?)");
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int disable(String appMd5, Boolean disable, String userLoginName) {
		StringBuilder sql = new StringBuilder("UPDATE app_version_detail  SET  disable =?");
		List<Object> params = new ArrayList<>();
		params.add(disable);
		sql.append(" WHERE ");
		sql.append("app_md5 = ?");
		params.add(appMd5);
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int delete(String id) {
		StringBuilder sql = new StringBuilder(" DELETE FROM app_version_detail WHERE ");
		List<Object> params = new ArrayList<>();
		sql.append("id = ?");
		params.add(id);
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public int delete(List<String> ids) {
		if (ids == null || ids.isEmpty()) {
			return 0;
		}
		StringBuilder sql = new StringBuilder("DELETE FROM app_version_detail WHERE ");
		List<Object> params = new ArrayList<>();
		sql.append("id in (");
		for (int i = 0; i < ids.size(); i++) {
			String id = ids.get(i);
			if (i == 0) {
				sql.append("?");
			} else {
				sql.append(",?");
			}
			params.add(id);
		}
		sql.append(")");
		int update = jdbcTemplate.update(sql.toString(), params.toArray());
		return update;
	}

	public AppVersionDetailEntity findLastPublishOne(String appPackage) {
		String sql = "select * from app_version_detail where publish_flag =1 and app_package =? order by version_code desc limit 1";
		try {
			return jdbcTemplate.queryForObject(sql.toString(),
			        new BeanPropertyRowMapper<AppVersionDetailEntity>(AppVersionDetailEntity.class), appPackage);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	public AppVersionDetailEntity findByAppKeyVersion(String appPackage, Integer appVersion) {
		String sql = "select * from app_version_detail where publish_flag = 1 and app_package =? and appVersion =?order by version_code desc limit 1";
		try {
			return jdbcTemplate.queryForObject(sql.toString(),
			        new BeanPropertyRowMapper<AppVersionDetailEntity>(AppVersionDetailEntity.class), appPackage,
			        appVersion);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}
	
	public AppVersionDetailEntity findOne(Integer id) {
		String sql = "select * from app_version_detail where  id = ? order by version_code desc limit 1";
		try {
			return jdbcTemplate.queryForObject(sql.toString(),
			        new BeanPropertyRowMapper<AppVersionDetailEntity>(AppVersionDetailEntity.class), id);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

}
